package service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import domain.Black;
import domain.User;
import domain.example.BlackExample;
import service.IBlackService;
import service.IBlackTranService;
import service.IUserService;

@Service("BlackTranService")
public class BlackTranServiceImpl implements IBlackTranService {
	@Resource
	private IBlackService blackService;
	@Resource
	private IUserService userService;
	
	@Override
	public boolean appendBlack(Integer userId, Integer targetId) {
		if (userId == null || targetId == null) {
			return false;
		}
		
		Black black = new Black();
		black.setOwnId(userId);
		black.setUserId(targetId);
		
		int result = 0;
		try {
			result = blackService.insert(black);
		} catch (Exception e) {
		}
		
		return result == 1;
	}
	
	@Override
	public boolean moveOutBlack(Integer userId, Integer targetId) {
		if (userId == null || targetId == null) {
			return false;
		}
		
		BlackExample example = new BlackExample();
		example.createCriteria().andOwnIdEqualTo(userId).andUserIdEqualTo(targetId);
		
		return blackService.deleteByExample(example) == 1;
	}

	@Override
	public List<User> getBlackList(Integer userId) {
		if (userId == null) {
			return null;
		}
		
		BlackExample example = new BlackExample();
		example.createCriteria().andOwnIdEqualTo(userId);
		List<Black> blacks = blackService.selectByExample(example);
		
		if (blacks == null) {
			return null;
		}
		
		List<User> result = new ArrayList<User>();
		
		for (Black b : blacks) {
			if (b.getUserId() == null) {
				continue;
			}
			User u = userService.selectByPrimaryKey(b.getUserId());
			if (u != null) {
				User tUser = new User();
				tUser.setUserId(u.getUserId());
				tUser.setUserName(u.getUserName());
				result.add(tUser);
			}
		}
		
		return result;
	}

}
